package com.jason.quectel0805.activities;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jason.quectel0805.R;
import com.jason.quectel0805.net.IOUtils;

import java.io.File;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class SDCardActivity extends AppCompatActivity {
    private static final String TAG = "jasonwan";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdcard);
    }

    /**
     * 访问所有私有目录
     *
     * @param view
     */
    public void accessPrivateDir(View view) {
        File cacheDir = getCacheDir();
        File filesDir = getFilesDir();
        Log.d(TAG, "getCacheDir(): " + cacheDir.getAbsolutePath());
        Log.d(TAG, "getFilesDir(): " + filesDir.getAbsolutePath());
    }

    /**
     * 访问公有目录
     *
     * @param view
     */
    public void accessPublicDir(View view) {
        File externalCacheDir = getExternalCacheDir();
        File externalFilesDir = getExternalFilesDir(null);
        Log.d(TAG, "getExternalCacheDir(): " + externalCacheDir.getAbsolutePath());
        Log.d(TAG, "getExternalFilesDir(): " + externalFilesDir.getAbsolutePath());
    }

    /**
     * 往私有目录里面写数据
     *
     * @param view
     */
    public void writeData2PrivateDir(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File filesDir = getFilesDir();
                File file = new File(filesDir, "test.txt");
                boolean result = IOUtils.writeData2PrivateDir("hello", file.getAbsolutePath());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result) {
                            Toast.makeText(SDCardActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SDCardActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }

    /**
     * 往公有目录写入数据
     *
     * @param view
     */
    public void writeData2PublicDir(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File filesDir = getExternalFilesDir(null);
                File file = new File(filesDir, "test.txt");
                boolean result = IOUtils.writeData2PrivateDir("hello", file.getAbsolutePath());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result) {
                            Toast.makeText(SDCardActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SDCardActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }

    /**
     * 访问公有目录的根目录
     * <p>
     * 访问SDcard的根目录是一个比较危险的行为，在Android6.0之后，如果需要访问根目录，需要动态申请SDCard权限
     *
     * @param view
     */
    public void accessPublicRootDir(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                accessPublicRootDir();
            } else {
                //    没有权限，那就申请权限
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
            }
        }
    }

    private void accessPublicRootDir() {
        File rootFile = Environment.getExternalStorageDirectory();
        Log.d(TAG, "Environment.getExternalStoragePublicDirectory(): " + rootFile.getAbsolutePath());
        new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File(rootFile, "test.txt");
                boolean result = IOUtils.writeData2PrivateDir("hello", file.getAbsolutePath());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result) {
                            Toast.makeText(SDCardActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SDCardActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }

    /**
     * 权限申请回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
            accessPublicRootDir();
        } else {
            Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
        }
    }
}